import { createRouter, createWebHistory } from "vue-router";

const routes = [
    {
    path: '/PostEditor',
    name: 'PostEditor',
    component: () => import('../view/PostEditor/index.vue'),
    meta: { title:'帖子发表' } // 可根据需要添加权限控制
  },
  {
    path: '/',
    redirect: '/home/index', // 修正重定向路径
    meta: { title: '校园集市平台666' } 
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../view/login/login.vue'),
    meta: { title: '登录 - 校园集市平台' } 
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../view/register/register.vue'),
    meta: { title: '注册 - 校园集市平台' } 
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../components/layout/index.vue'),
    children: [
      {
        path: 'index',
        name: 'index',
        redirect: { name: 'topic-recommend' }, // 命名路由重定向
        component: () => import('../view/index/index.vue'),
        meta: { title: '首页 - 校园集市平台' },
        children: [
          {
            path: 'topic-trade',
            name: 'topic-trade',
            component: () => import('../view/index/topic-trade/index.vue')
          },
          {
            path: 'topic-help',
            name: 'topic-help',
            component: () => import('../view/index/topic-help/index.vue')
          },
          {
            path: 'topic-recommend',
            name: 'topic-recommend',
            component: () => import('../view/index/topic-recommend/index.vue')
          },
          {
            path: 'topic-love',
            name: 'topic-love',
            component: () => import('../view/index/topic-love/index.vue')
          },
          {
            path: 'topic-campus',
            name: 'topic-campus',
            component: () => import('../view/index/topic-campus/index.vue')
          },
          {
            path: 'topic-recruit',
            name: 'topic-recruit',
            component: () => import('../view/index/topic-recruit/index.vue')
          },
        ]
      },
      {
        path: 'person',
        name: 'person',
        component: () => import('../view/person/person.vue'),
        meta: { title: '个人中心 - 校园集市平台' }
      },
      {
        path: 'explore',
        name: 'explore',
        component: () => import('../view/explore/index.vue'),
        meta: { title: '搜索 - 校园集市平台' } 
      },
      {
        path: 'message',
        name: 'message',
        component: () => import('../view/message/message.vue'),
        meta: { title: '私信 - 校园集市平台' } 
      },
      {
        path: 'details',
        name: 'details',
        component: () => import('../view/details/index.vue')
      }
    ]
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

router.beforeEach((to) => {
  if (to.meta.title) {
    document.title = to.meta.title;
  } else {
    document.title = '校园集市平台';
  }
});

export default router;